#include "xmppcore/errordetailsparserfactory.h"
#include "xmppcore/xmllangtext.h"
#include "xmppcore/stanzaerror.h"
#include "xmppcore/streamerror.h"

namespace yoohoo_xmppcore {

const QString ErrorDetailsParser::PATH_TEXT = "text";
const QString ErrorDetailsParser::ATTRIBUTE_LOCAL_NAME_LANG = "lang";
const QString ErrorDetailsParser::ATTRIBUTE_NAMESPACE_XML = "xml";
const QString ErrorDetailsParser::ATTRIBUTE_LOCAL_NAME_XMLNS = "xmlns";

ErrorDetailsParser::ErrorDetailsParser() : _obj(0) {
}

XmppObject* ErrorDetailsParser::obj() {
	return _obj;
}

bool ErrorDetailsParser::processAttributes(QString path, QList<ParsingAttribute> attributes) {
	if (PATH_TEXT == path) {
		if (attributes.size() > 1) {
			return false;
		}

		XmlLangText* text = new XmlLangText;
		if (attributes.size() == 1) {
			if (ATTRIBUTE_NAMESPACE_XML != attributes[0].prefix() ||
					ATTRIBUTE_LOCAL_NAME_LANG != attributes[0].localName()) {
				return false;
			}

			text->setXmlLang(attributes[0].value());
		}

		_obj = text;
	} else {
		if (attributes.size() != 0) {
			return false;
		}
	}

	return true;
}

bool ErrorDetailsParser::processText(QString path, QString text) {
	if (PATH_TEXT == path) {
		((XmlLangText*)_obj)->setText(text);
	} else {
		_obj = new ErrorCondition(path);
	}

	return true;
}

XmppParser* ErrorDetailsParserFactory::createParser() {
	return new ErrorDetailsParser;
}

ProtocolKey StanzaErrorDetailsParserFactory::protocolKey() {
	return StanzaError::ERROR_DETAIL_PROTOCOL_KEY;
}

ProtocolKey StreamErrorDetailsParserFactory::protocolKey() {
	return StreamError::ERROR_DETAIL_PROTOCOL_KEY;
}

}
